import { requestClient } from '#/api/request';

const AppOptions = import.meta.env;

export namespace GeoFileApi {
  export interface DeleteOrImportShp {
    userId: string;
    catalogId: string;
    fileId: string;
  }
}
const getGeoDataList = (userId: string) => {
  return requestClient.get(`/geofile?userId=${userId}`);
};
const getGeoPreview = (fileId: any) => {
  return requestClient.get(`/geofile/getShpPreview?fileId=${fileId}`);
};

const getShpFeatures = (fileName: any) => {
  return requestClient.get(`/geofile/getAtrr?shpName=${fileName}`);
};
const getFields = (geoId: string) => {
  return requestClient.get(`/geofile/getFields?geoId=${geoId}`);
};
const getUniqueValues = (ptName: string, field: string, method: string) => {
  return requestClient.get(
    `/geofile/getUniqueValues?ptName=${ptName}&field=${field}&method=${method}`,
  );
};
const getFeatureCount = (ptName: string) => {
  return requestClient.get(`/geofile/getFeatureCount?ptName=${ptName}`);
};
const uploadShp = (uploadFormData: any, config: any) => {
  return requestClient.upload('/geofile', uploadFormData, config);
};

// const deleteShp = (deleteObj: GeoFileApi.DeleteOrImportShp) => {
//   return requestClient.delete('/geofile', deleteObj);
// };

const downloadFile = (fileId: any) => {
  return new Promise(
    (resolve: (value: unknown) => void, reject: (reason?: any) => void) => {
      try {
        window.location.href = `${AppOptions.VITE_BACKEND_IP}/geofile/download?fileId=${fileId}`;
        // requestClient.download('requestClient://localhost:8998/file/download?fileId=' + fileId)
        resolve(true);
      } catch (error) {
        reject(error);
      }
    },
  );
};

const importShpResource = (
  importShpResourceObj: GeoFileApi.DeleteOrImportShp,
) => {
  return requestClient.post('/geofile/importShpResource', importShpResourceObj);
};
export default {
  getGeoDataList,
  uploadShp,
  downloadFile,
  getGeoPreview,
  getShpFeatures,
  getFields,
  getUniqueValues,
  getFeatureCount,
  importShpResource,
};
